﻿package com.sh.game.pool
{
    import flash.utils.*;

    public class ObjectPoolManager extends Object
    {
        private var pools:HashMap;
        private var _maxDic:Dictionary;
        private static var _instance:ObjectPoolManager;

        public function ObjectPoolManager(param1:Sig)
        {
            this.pools = new HashMap();
            this._maxDic = new Dictionary();
            return;
        }// end function

        public function initPool(param1:String, param2:int) : void
        {
            if (this.pools.containsKey(param1))
            {
                return;
            }
            var _loc_3:Array = [];
            this.pools.add(param1, _loc_3);
            this._maxDic[param1] = param2;
            return;
        }// end function

        public function returnItem(param1:String, param2:IPoolItem) : void
        {
            if (!this.pools.containsKey(param1))
            {
                throw new Error("Not find：" + param1 + " pool");
            }
            param2.reset();
            var _loc_3:* = this.pools.getValue(param1);
            if (this._maxDic[param1] > _loc_3.length)
            {
                _loc_3.push(param2);
            }
            else
            {
                param2.dispose();
            }
            return;
        }// end function

        public function borrowItem(param1:String) : IPoolItem
        {
            var _loc_2:Class = null;
            if (!this.pools.containsKey(param1))
            {
                throw new Error("Not find：" + param1 + " pool");
            }
            if ((this.pools.getValue(param1) as Array).length == 0)
            {
                _loc_2 = getDefinitionByName(param1) as Class;
                return new _loc_2;
            }
            return (this.pools.getValue(param1) as Array).pop();
        }// end function

        public function disposePool(param1:String) : void
        {
            if (!this.pools.containsKey(param1))
            {
                return;
            }
            var _loc_2:* = this.pools.getValue(param1) as Array;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                (_loc_2[_loc_3] as IPoolItem).dispose();
                _loc_3++;
            }
            _loc_2 = null;
            this.pools.remove(param1);
            return;
        }// end function

        public static function getInstance() : ObjectPoolManager
        {
            if (!_instance)
            {
                _instance = new ObjectPoolManager(new Sig());
            }
            return _instance;
        }// end function

    }
}

final class Sig extends Object
{

    function Sig()
    {
        return;
    }// end function

}

